﻿// ***********************************************************************
// 程 序 集  : HdhNetQuery
// 建立时间  : 2025-10
// 类库说明  ：可实例化数据操作类
// 个人网站  ：http://www.hdhcms.com 
// ***********************************************************************
// <copyright>
//     Copyright by He DongHua(何冬华) All, QQ群:876705805 QQ:664627440 Email:664627440@qq.com
// </copyright>
// 如果要使用当前代码，请保存这些信息
// ***********************************************************************
using System.Data;

namespace HdhNetQuery
{
    public class HdhcmsDataHelperObject
    {
        private int DataType;
        public IHdhcmsHelper hdhcmsHelper;
        public HdhcmsDataHelperObject() 
        { 
            DataType= HdhcmsUniversalFunction.GetDataType();
            switch (DataType)
            {
                case (int)HdhcmsEnumClass.DataType.Oracle:
                    hdhcmsHelper = new HdhcmsOracleHelper();
                    break;
                 case (int)HdhcmsEnumClass.DataType.DMdata:
                    hdhcmsHelper = new HdhcmsDMsqlHelper();
                    break;
               case (int)HdhcmsEnumClass.DataType.GaussDB:
                    hdhcmsHelper = new HdhcmsGaussDBHelper();
                    break;
                case (int)HdhcmsEnumClass.DataType.PostgreSql:
                    hdhcmsHelper = new HdhcmsNpgsqlHelper();
                    break;
                case (int)HdhcmsEnumClass.DataType.Mssql:
                    hdhcmsHelper = new HdhcmsMssqlHelper();
                    break;
                case (int)HdhcmsEnumClass.DataType.MySql:
                    hdhcmsHelper = new HdhcmsMySqlHelper();
                    break;
                default://HdhcmsEnumClass.DataType.Sqlite
                    hdhcmsHelper=new HdhcmsSQLiteHelper();
                    break;
            }
        }
        /// <summary>
        /// 返回连接字符串
        /// </summary>
        /// <returns></returns>
        public string? GetConnString()
        {
            return hdhcmsHelper.GetConnString();
        }
        /// <summary>
        /// 增、删、改的专用执行方法 
        /// </summary>
        /// <param name="Npgsql">Npgsql语句</param>
        /// <param name="param">参数数组 NpgsqlParameter类型</param>
        /// <returns>操作受影响的行数</returns>
        /// <exception cref="Exception">抛出的异常</exception>
        public int ExecuteNonQuery(string Npgsql, object[]? param = null)
        {
            return hdhcmsHelper.ExecuteNonQuery(Npgsql, param);
        }
        /// <summary>
        /// 插入数据并返回最新的自动编号ID
        /// </summary>
        /// <param name="Npgsql">Npgsql语句</param>
        /// <param name="param">参数数组 NpgsqlParameter类型</param>
        /// <returns>返回当前插入数据的自动编号</returns>
        /// <exception cref="Exception">抛出的异常</exception>
        public int InsertDataGetAutoId(string Npgsql, object[]? param = null)
        {
           return hdhcmsHelper.InsertDataGetAutoId(Npgsql, param);
        }
        /// <summary>
        /// 聚合查询返回单一结果
        /// </summary>
        /// <param name="Npgsql">Npgsql语句</param>
        /// <param name="param">参数数组 NpgsqlParameter类型</param>
        /// <returns>返回当前聚合查询的结果</returns>
        /// <exception cref="Exception">抛出的异常</exception>
        public object? ExecuteScalar(string Npgsql, object[]? param = null)
        {
            return hdhcmsHelper.ExecuteScalar(Npgsql, param);
        }
        /// <summary>
        /// 获取一个DATATABLE查询结果集
        /// </summary>
        /// <param name="Npgsql">Npgsql语句</param>
        /// <param name="param">参数数组 NpgsqlParameter类型</param>
        /// <returns>返回DataTable对象</returns>
        /// <exception cref="Exception">抛出的异常</exception>
        public DataTable GetDataTable(string Npgsql, object[]? param = null)
        {
            return hdhcmsHelper.GetDataTable(Npgsql, param);
        }
        /// <summary>
        /// 返回DATASET，用0,1,2,3区分不同的DATATABLE
        /// </summary>
        /// <param name="Npgsql"></param>
        /// <param name="param"></param>
        /// <returns>返回DataSet对象</returns>
        /// <exception cref="Exception">抛出的异常</exception>
        public DataSet GetDataSet(string Npgsql, object[]? param = null)
        {
            return hdhcmsHelper.GetDataSet(Npgsql,param);
        }
        /// <summary>
        /// 返回多个可命名DataTable的DATASET
        /// </summary>
        /// <param name="NpgsqlDic">语句字典，包括名称及语句</param>
        /// <param name="dicParam">参数字典，期中key与语句字典的key一致</param>
        /// <returns>返回DataSet对象</returns>
        /// <exception cref="Exception">抛出的异常</exception>
        public DataSet GetDataSet(Dictionary<string, string> NpgsqlDic, Dictionary<string, object[]>? dicParam = null)
        {
            return hdhcmsHelper.GetDataSet(NpgsqlDic, dicParam);
        }
        /// <summary>
        /// 通过事务批量执行Npgsql查询语句
        /// </summary>
        /// <param name="NpgsqlList">语句语句集合</param>
        /// <returns>成功返回TRUE，否则FALSE</returns>
        public string ExecuteNonQueryTrans(List<string> NpgsqlList)
        {
            return hdhcmsHelper.ExecuteNonQueryTrans(NpgsqlList);
        }
        /// <summary>
        /// 通过事务批量执行Npgsql查询语句，有参数时要全部都有，参数要对等
        /// </summary>
        /// <param name="NpgsqlList"></param>
        /// <param name="paran">参数必填，必须与NpgsqlList形成对应的关系 </param>
        /// <returns></returns>
        public string ExecuteNonQueryTrans(List<string> NpgsqlList, List<object[]>? paran = null)
        {
            return hdhcmsHelper.ExecuteNonQueryTrans(NpgsqlList, paran);
        }

        /// <summary>
        /// 根据查询结果返回List集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strNpgsql"></param>
        /// <param name="paran"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public List<T> GetQueryList<T>(string strNpgsql, object[]? paran = null) where T : new()
        {
            return hdhcmsHelper.GetQueryList<T>(strNpgsql, paran);
        }
        /// <summary>
        /// 根据查询返回Model
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strNpgsql"></param>
        /// <param name="paran"></param>
        /// <returns>返回Model</returns>
        /// <exception cref="Exception"></exception>
        public T GetQueryModel<T>(string strNpgsql, object[]? paran = null) where T : new()
        {
            return hdhcmsHelper.GetQueryModel<T>(strNpgsql, paran);
        }
        /// <summary>
        /// 查询获取DataReader对象
        /// </summary>
        /// <param name="Npgsql">Npgsql语句</param>
        /// <param name="param">参数数组 NpgsqlParameter类型</param>
        /// <returns>返回一个DataReader对象</returns>
        /// <exception cref="Exception">抛出的异常</exception>
        public object GetExecuteReader(string Npgsql, object[]? param = null)
        {
            return hdhcmsHelper.GetExecuteReader(Npgsql, param);
        }
        /// <summary>
        /// 表是否存在
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public bool TableExists(string TableName)
        {
            return hdhcmsHelper.TableExists(TableName);
        }
        /// <summary>
        /// 表的字段是否存在
        /// </summary>
        /// <param name="TableName">表名</param>
        /// <param name="FieldName">字段表</param>
        /// <returns></returns>
        public bool TableFieldExists(string TableName, string FieldName)
        {
            return hdhcmsHelper.TableFieldExists(TableName,FieldName);
        }
    }
}
